#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "../include/meta.h"


node* createTree(){
	node *root= (node *) malloc(sizeof(node));
	strcpy(root->data,"home");
	root->index = -1;
	root->lchild=NULL;
	root->rchild=NULL;
	root->parent=NULL;
	return root;
}

node* insertNode(char* value, int index)
{	node *new= (node *) malloc(sizeof(node));
	 strcpy(new->data,value);
	 new->index = index;
	 new->lchild=NULL;
	 new->rchild=NULL;
	 new->parent = NULL;
	return new;
}

void insertIntoTree(char str[50], node *root,int flag){
	int index;
	node *prev= (node *) malloc(sizeof(node));
	node* temp=(node*) malloc(sizeof(node));

	//splitting full path at '/'
	char *token;
	token=strtok(str,"/");

	if(strcmp(root->data,token)!=0)
		{printf("Wrong path\n");
		 return;
		}

	while(token!=NULL)
	{
	if(root==NULL)
		{
		if(flag == 1)
			index = getDirIndex();
		else
			index = getFileIndex();
		temp=insertNode(token,index);
		prev->lchild=temp;
		temp->parent = prev;
		 prev=prev->lchild;
		 root=prev->lchild;
		 
		}

	else if(strcmp(root->data,token)==0) // match
		{prev=root;
		 
		 root=root->lchild;	 
		}
	else if(strcmp(root->data,token)!=0) // no match
		{
		while((root->rchild!=NULL)&&(strcmp(root->data,token)!=0))
			root=root->rchild;
		 if(strcmp(root->data,token)==0)
		 {
		  prev = root;
		  root=root->lchild;
		 }
		 else
		 {
			 if(flag == 1)
				index = getDirIndex();
			else
				index = getFileIndex();
		  temp=insertNode(token,index);
		  root->rchild=temp;
		  temp->parent = root->parent;
		 
		  prev=root->rchild;
		  root=prev->lchild;
		 }
		}
	token=strtok(NULL,"/");
	}
	//return temp;
}

void displayTree(node *root){

	if(root!=NULL){
	
	displayTree(root->lchild);
	printf(" %s",root->data);
	printf("(parent=%s)",root->parent->data);
	displayTree(root->rchild);
	}	
}


void listnodes(char str[50], node *root){

	node *prev= (node *) malloc(sizeof(node));
	//splitting full path at '/'
	char *token;
	token=strtok(str,"/");

	if(strcmp(root->data,token)!=0)
		{printf("Wrong path\n");
		 return;
		}

	while(token!=NULL)
	{
	if(root==NULL)
		{
			//printf("error in file path, no path found\n");
		}

	else if(strcmp(root->data,token)==0) // match
		{prev=root;
		 root=root->lchild;	 
		}
	else if(strcmp(root->data,token)!=0) // no match
		{
		while((root->rchild!=NULL)&&(strcmp(root->data,token)!=0))
			root=root->rchild;
		 if(strcmp(root->data,token)==0)
			root=root->lchild;
		}
	token=strtok(NULL,"/");
	}	
	while(root!=NULL)
	{
		printf("\n%s", root->data);
		root = root->rchild;
	}
	
}


int ifexists(char str[50], node *root,char name[20]){

	node *prev= (node *) malloc(sizeof(node));
	//splitting full path at '/'
	char *token;
	token=strtok(str,"/");
	while(token!=NULL)
	{
	if(root==NULL)
		{
			return 0;
		}

	else if(strcmp(root->data,token)==0) // match
		{prev=root;
		 root=root->lchild;	 
		}
	else if(strcmp(root->data,token)!=0) // no match
		{
		while((root->rchild!=NULL)&&(strcmp(root->data,token)!=0))
			root=root->rchild;
		 if(strcmp(root->data,token)==0)
			root=root->lchild;
		}
	token=strtok(NULL,"/");
	}	
	while(root!=NULL)
	{
		if(strcmp(root->data,name)==0)
			return 1;
		root = root->rchild;
	}
	return 0;
}



void move(char src[50],char dest[50], node *root)
{	node *srcnode= (node *) malloc(sizeof(node));
	node *destnode = (node *) malloc(sizeof(node));
	node *rootnode = (node *) malloc(sizeof(node));
	node *prev= (node *) malloc(sizeof(node));
	node *temp,*folder;
	rootnode = root;
	//srcnode = root;
	//splitting full path at '/'
	char *token;
	token=strtok(src,"/");

	if(strcmp(root->data,token)!=0)
		{printf("Wrong source path\n");
		 return;
		}

	while(token!=NULL)
	{
		if(rootnode==NULL)
		{
			printf("error in file path, no path found\n");
		}

		else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		 rootnode=rootnode->lchild;	 
		}
		else if(strcmp(rootnode->data,token)!=0) // no match
		{

		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;
			rootnode=rootnode->lchild;
			}
		}
	token=strtok(NULL,"/");
	}
	srcnode = prev;
	
	rootnode = root;
	token=strtok(dest,"/");
	if(strcmp(root->data,token)!=0)
		{printf("Wrong destination path\n");
		 return;
		}

	while(token!=NULL)
	{
	if(rootnode==NULL)
		{
			printf("error in file path, no path found\n");
		}

	else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		 rootnode=rootnode->lchild;	 
		}
	else if(strcmp(rootnode->data,token)!=0) // no match
		{
		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;
			rootnode=rootnode->lchild;
			}
		}
	token=strtok(NULL,"/");
	}
	destnode = prev;

	// moving a folder into itself
	folder=destnode;
	temp=folder->parent;
	while(folder!=NULL && temp!=srcnode)
	{	folder=folder->parent;
		temp=folder->parent;
	}
	if(temp==srcnode)
	{	printf("Cannot move a folder into itself\n");
		return;
	}

	// src is lchild
	temp=srcnode->parent;
	if(srcnode= temp->lchild)
		temp->lchild= srcnode->rchild;
	
	// src is rchild
	else{ 
		prev=temp->lchild;
		while(prev->rchild!=srcnode)
		{ prev=prev->rchild;}
		prev->rchild=srcnode->rchild;
	}
	srcnode->rchild = NULL;

	//actual moving code
	if(destnode->lchild==NULL)
		{destnode->lchild=srcnode;
		 srcnode->parent=destnode;
		}
	 else
		{
		
		 destnode=destnode->lchild;
		 while(destnode->rchild!=NULL)
			destnode=destnode->rchild;
		 destnode->rchild=srcnode;
		 srcnode->parent=destnode->parent;
		}
}


void delete(char src[50], node *root)
{	node *srcnode= (node *) malloc(sizeof(node));
	node *rootnode=root;
	node* temp=(node*) malloc(sizeof(node));
	node *prev= (node *) malloc(sizeof(node));
	rootnode=root;

	char *token;
	char temp_src[50];
	strcpy(temp_src,src);

	token=strtok(temp_src,"/"); 
	if(strcmp("home",token)==0)
		{token=strtok(NULL,"/"); 
		 if(token==NULL) 
			{printf("Cannot delete root\n");
		 	 return;
			}
		}

	
	token=strtok(src,"/");
	if(strcmp(root->data,token)!=0)
		{printf("Wrong path\n");
		 return;
		}

	while(token!=NULL)
	{
		if(rootnode==NULL)
		{
			printf("error in file path, no path found\n");
		}

		else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		 rootnode=rootnode->lchild;	 
		}
		else if(strcmp(rootnode->data,token)!=0) // no match
		{

		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;
			rootnode=rootnode->lchild;
			}
		}
	token=strtok(NULL,"/");
	}
	srcnode = prev;
	
	//delete srcnode
	
	// src is lchild
	temp=srcnode->parent;	
	
	if(srcnode= temp->lchild)
	{
	temp->lchild= srcnode->rchild;
	}	
	// src is rchild
	else{ 
		prev=temp->lchild;
		while(prev->rchild!=srcnode)
		{ prev=prev->rchild;}
		prev->rchild=srcnode->rchild;
	}
	free(srcnode);

}


node* checkpath_returnnode(char str[50], node *root){

	node *prev= (node *) malloc(sizeof(node));
	//splitting full path at '/'
	char *token;
	token=strtok(str,"/");

	if(strcmp(root->data,token)!=0)
		{printf("Wrong path\n");
		 return NULL;
		}

	while(token!=NULL)
	{
	if(root==NULL)
		{
			return NULL;
		}

	else if(strcmp(root->data,token)==0) // match
		{prev=root;
		 root=root->lchild;	 
		}
	else if(strcmp(root->data,token)!=0) // no match
		{
		while((root->rchild!=NULL)&&(strcmp(root->data,token)!=0))
			root=root->rchild;
		 if(strcmp(root->data,token)==0)
			{
			prev=root;
			root=root->lchild;
			}
		else
			return NULL;
		}
	token=strtok(NULL,"/");
	}
	return prev;
}



/*int main(){
	
	node* root=createTree();	
	char str[50],src[50],dest[50]; int i,index;

do{
printf("Enter full path of file to be created\n");
scanf("%s %d",str,&index);
	insertIntoTree(str, root,index);
printf("Press 1 to exit  ");
scanf("%d",&i);
}while(i!=1);
	
	displayTree(root);
	//char abc[]="home/abc";
	//listnodes(abc,root);
/*
	printf("\nenter source and destination\n");
	scanf("%s%s",src,dest);
	move(src,dest, root);
	displayTree(root);
	printf("\n");

	printf("\nenter file to be deleted\n");
	scanf("%s",src);
	delete(src, root);
	displayTree(root);
	printf("\n");
	//displayTreepre(root);
return 0;
}*/
